package cn.flc.web.Servlet;

import cn.flc.bean.User;
import cn.flc.mapper.UserMapper;
import cn.flc.util.MyException;
import cn.flc.util.SqlSessionFactoryUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession sqlSession=null;
        //密码长度为8到20位,必须包含字母和数字，字母区分大小写
        try {
            //设置响应编码防止乱码
            resp.setContentType("text/html;charset=utf-8");
            //设置数据库接收用户乱码
            req.setCharacterEncoding("utf-8");
            //接收用户名,密码
            String username = req.getParameter("username");
            if(StrUtil.hasEmpty(username)){
                throw new MyException("用户名为空");
            }
            //     System.out.println(username);
            String password = req.getParameter("password");
            String regEx1 = "^(?=.*[0-9])(?=.*[a-zA-Z])(.{8,20})$";
            Pattern Password_Pattern = Pattern.compile(regEx1);
            Matcher matcher = Password_Pattern.matcher(password);
            System.out.println(password);
            if(StrUtil.hasEmpty(password)||(!matcher.matches())){
                throw new MyException("密码为空或者不符合规范");
            }
            //封装用户对象
            User user1 = new User();
            user1.setUsername(username);
            user1.setPassword(password);
            //调用mybatis查询用户
            //获取sqlsession
            SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
            //执行sql
            sqlSession = sqlSessionFactory.openSession();
            //获取USerMapper的代理对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //查是否存在
            User user = mapper.selectByUserName(username);
            //准备打印
            PrintWriter writer = resp.getWriter();
            //判断是否成功
            if(user!=null){
                writer.write("用户已存在");
            }else{
               mapper.add(user1);
               writer.write("注册成功");

            }
        } catch (UnsupportedEncodingException | MyException e) {
            e.printStackTrace();
        }finally {
            //关闭sqlsession
            //提交事务
            sqlSession.commit();
            sqlSession.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
